<?php


namespace app\admin\controller;


use app\api\service\JinDie;
use app\common\controller\AdminBaseController;

use app\common\model\tang\BalanceLog;
use app\common\model\tang\Cost;
use app\common\model\tang\CostType;
use app\common\model\tang\Merchant;

use think\cache\driver\Redis;
use think\Db;
use app\common\model\tang\KwhLog;
use app\common\model\tang\MonthElectricityDegree;
use app\common\model\tang\MonthWaterLog;
use think\facade\Queue;
use app\admin\service\ProofService;


class Proof extends AdminBaseController
{
    protected function initialize()
    {

        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\common\model\tang\Proof();
    }


    public function index()
    {
        if (IS_AJAX) {
            return $this->model->getAdminPageData($this->param);
        }
        $merchant_list = getMerchant();
        //获取菜单列表
        $this->assign(compact("merchant_list"));
        return $this->fetch();
    }

    public function delToken()
    {

        $redis = (new Redis(config('queue.connections.redis')));
        $redis->delete('JINDIE');
        return self::ajaxResult([], 0, 0, '清除成功');
    }

    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $jin = new JinDie();
            $proofService = new ProofService();
            if (isset($data['start_month']) && !empty($data['start_month'])) {
                $data['start_time'] = $data['start_month'];
            }
            if (empty($data['start_time']) || empty($data['type'])) {
                return self::ajaxResult([], 0, 0, '请选择凭证类型/时间');
            }
//            $bookedDate = date($data['start_month'].'-t');
//            $bookedDate = date("Y-m-t", strtotime($data['start_month']));
            $bookedDate = date("Y-m-d");
            $month = date("m");
            $time = explode('-', $data['start_time']);
            if ($data['type'] == 1) { //历史电费
                $type = '电费';
                $costInfo = (new Cost())->with(['merchant', 'costType'])->whereNotIn('merchant_id',[222,1916,1917,1918])->whereNotNull('pointId')->where('all_amount', '>', 0)->where('settlement_time', $data['start_month'])->where(['is_push' => 0, 'cost_type_id' => 45])->select();
                if ($costInfo->isEmpty()) {
                    return self::ajaxResult([], 0, 0, $data['start_month'] . '没有待推送的电费凭证');
                }
                $code = $costInfo[0]['merchant']['code'];
                $start = $costInfo[0]['settlement_time'];
                $end = $costInfo[count($costInfo) - 1]['settlement_time'];
                $proof = [];
                $BLMoney = 0; //合计不含税电费
                $dianBLMoney = 0; //合计不含税费
                $FWMoney = 0; //合计不含税服务费
                $dianFWMoney = 0; //合计服务费税费
                foreach ($costInfo as $k => $v) {
                    $brand = '';
                    if (!empty($v['merchant']['name']) && $v['merchant']['name'] != '-') {
                        $brand = '(' . $v['merchant']['name'] . ')';
                    }
                    $brand = str_replace('&', '-', $brand);
                    $Year = explode('-', $v['settlement_time']);
                    if ($v['cost_type_id'] == 45 || $v['cost_type_id'] == 46) {
                        $voucherType = '转';
                    } else {
                        $voucherType = '记';
                    }
                    if ($v['cost_type_id'] == 45) {
                        $voucherAbstract = '电费';
                    }
                    if ($v['cost_type_id'] == 46) {
                        $voucherAbstract = '水费';
                    }
                    $dianfei = TaxMoney($v['electric_money'] / 100, 1, 13);
                    $BLMoney += $dianfei['money'];
                    $dianBLMoney += $dianfei['tax_money'];
                    $accountNumber = $v['costType']['cost_sn'];
                    $originalAmount = $dianfei['money'];
                    //服务费=====增幅15%的未进行计算
                    $fuwu = TaxMoney($v['fuwu_money'] / 100, 1, 6);
                    $FWMoney += $fuwu['money'];
                    $dianFWMoney += $fuwu['tax_money'];
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0120",//凭证号
                        "entrySeq" => $k + 1, //分录行号
                        "voucherAbstract" => "结转" . $Year[0] . '年' . $Year[1] . "月" . $v['merchant']['positions_code'] . $v['merchant']['tenant_name'] . ($v['pointId']) . $voucherAbstract . $brand,
                        "accountNumber" => $accountNumber,
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => $v['all_amount'] / 100,
                        "debitAmount" => $v['all_amount'] / 100,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => $v['merchant']['merchant_sn'],
                        "asstActName1" => $v['merchant']['tenant_name'],
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => '401001A' . $v['merchant']['positions_code'],
                        "asstActName4" => '401001A' . $v['merchant']['positions_name'],
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($BLMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0120",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
                        "voucherAbstract" => "结转" . $Year[0] . '年' . $Year[1] . "月电费",
                        "accountNumber" => '6401.03.02.02',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => '-' . $BLMoney,
                        "debitAmount" => '-' . $BLMoney,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "单元",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认单元",
                        "asstActType4" => "业态",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认业态",
                        "asstActType5" => "成本中心-部门",
                        "asstActNumber5" => "WYCB01",
                        "asstActName5" => "物业成本部",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($FWMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0120",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
                        "voucherAbstract" => "结转" . $Year[0] . '年' . $Year[1] . "月服务费",
                        "accountNumber" => '6401.03.02.04',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => '-' . $FWMoney,
                        "debitAmount" => '-' . $FWMoney,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "",
                        "asstActNumber1" => "",
                        "asstActName1" => "",
                        "asstActType2" => "",
                        "asstActNumber2" => "",
                        "asstActName2" => "",
                        "asstActType3" => "",
                        "asstActNumber3" => "",
                        "asstActName3" => "",
                        "asstActType4" => "",
                        "asstActNumber4" => "",
                        "asstActName4" => "",
                        "asstActType5" => "",
                        "asstActNumber5" => "",
                        "asstActName5" => "",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($dianBLMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0120",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
//                        "voucherAbstract" => "结转" .$Year[0].'年'. $Year[1] . "月电费税金"  ,
//                        "voucherAbstract" => "结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)). "电费税金"  ,
                        "voucherAbstract" => "结转" . $data['start_month'] . "电费税金",
                        "accountNumber" => '2221.01.01',
                        "currencyNumber" => "BB01",
                        "entryDC" => -1,
                        "originalAmount" => $dianBLMoney,
                        "debitAmount" => 0,
                        "creditAmount" => $dianBLMoney,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认单元",
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "税率",
                        "asstActNumber6" => "13",
                        "asstActName6" => "13%",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($dianFWMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0120",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
//                        "voucherAbstract" => "结转" .$Year[0].'年'. $Year[1] . "月服务费税金"  ,
//                        "voucherAbstract" => "结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)). "服务费税金"  ,
                        "voucherAbstract" => "结转" . $data['start_month'] . "服务费税金",
                        "accountNumber" => '2221.01.01',
                        "currencyNumber" => "BB01",
                        "entryDC" => -1,
                        "originalAmount" => $dianFWMoney,
                        "debitAmount" => 0,
                        "creditAmount" => $dianFWMoney,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认单元",
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "税率",
                        "asstActNumber6" => "6",
                        "asstActName6" => "6%",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                $res = $jin->importVoucher($proof);
                if (isset($res['code']) && $res['code'] == '0000') {
                    (new Cost())->where('settlement_time', $data['start_month'])->where(['is_push' => 0, 'cost_type_id' => 45])->update([
                        'is_push' => 1
                    ]);
                }
                unset($data['__token__']);
                $data['info'] = $res['message'];
                $data['code'] = $code;
                $data['push'] = '推送' . $data['start_time'] . $type . '凭证';
                $this->model->doAll($data);
            }
            if ($data['type'] == 2) { //充值记录导入金蝶
                $type = '充值记录';
                $balanceModel = new BalanceLog();
//                $nextDay = date('Y-m-d', strtotime($data['start_time']) + 86400);
                $nextDay = date('Y-m-d', strtotime($data['start_time']));
                $balanceInfo = $balanceModel->with(['merchant'])
                    ->where(['is_top_up' => 1, 'status' => 1, 'is_push' => 0])
                    ->whereBetweenTime('charge_time', strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59'))
                    ->select();//线下充值
                $secondInfo = $balanceModel->with(['merchant'])
                    ->where(['is_top_up' => 1, 'status' => 1, 'is_push' => 0])
                    ->whereNull('charge_time')
                    ->whereBetweenTime('create_time', strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59'))
                    ->select();//线下充值
                if ($balanceInfo->isEmpty() && $secondInfo->isEmpty()) {
                    return self::ajaxResult([], 0, 0, $data['start_time'] . '没有待推送的缴费记录凭证');
                } else {
                    if ($balanceInfo->isEmpty()) {
                        $balanceInfo = $secondInfo;
                    } elseif ($secondInfo->isEmpty()) {
                        $balanceInfo = $balanceInfo;
                    } else {
                        $balanceInfo = array_combine($balanceInfo, $secondInfo);
                    }
                }

                $code = $balanceInfo[0]['merchant']['code'];
                $proof = [];
                $proof0 = [];
                $proof1 = [];
                $proof2 = [];
                $onlineMonery = 0;
                $all_amount = 0;
                $onlinefuwu = 0;
                foreach ($balanceInfo as $key => $v) {
                    if (!empty($v['charge_time'])) {
                        $Year = explode('-', date('Y-m-d H:i', $v['charge_time']));
                    } else {
                        $Year = explode('-', $v['create_time']);
                    }
                    $brand = '';
                    if (!empty($v['merchant']['name']) && $v['merchant']['name'] != '-') {
                        $brand = '(' . $v['merchant']['name'] . ')';
                    }
                    $brand = substr_replace('&', '-', $brand);
                    $day = explode(' ', $Year[2]);
                    if ($v['type'] == 1) {
                        $all_amount+= $v['amount']/100;
//                        $fuwu = TaxMoney($v['amount'] / 100, 1, 0.6);
//                        $onlineMonery += $fuwu['money'];
//                        $onlinefuwu += $fuwu['tax_money'];
                        $onlinefuwu += round($v['amount'] / 100*0.006,2);
                    }
                    if ($v['type'] == 2) { //只有线上充值有1002.01
                        $accountNumber = '1002.01';
                        $entryDC = 1;
                        $originalAmount =$v['amount']/100 ;
                        $debitAmount = $v['amount']/100;
                        $creditAmount = 0;
                        $asstActNumber1='99999999';
                        $asstActName1 = '默认客户';
                        $asstActType3 = '银行账户';
                        $asstActNumber3='hf00005';
                        $asstActName3 = '兴业银行合肥胜利路支行-8888';
                        $asstActType4 = '品牌';
                        $asstActNumber4='99999999';
                        $asstActName4 = '默认品牌';
                        $asstActType5 = '单元';
                        $asstActNumber5='99999999';
                        $asstActName5 = '默认单元';
                        $asstActType6 = '业态';
                        $asstActNumber6='99999999';
                        $asstActName6 = '默认业态';
                        $asstActType7 = '银行';
                        $asstActNumber7='13.01';
                        $asstActName7 = '兴业银行合肥胜利路支行';
                        $arr = [
                            "companyNumber" => '0109',
//                    "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                    "bizDate" => date('Y-m-01'),//业务日期
//                    "periodYear" => date('Y'),//会计期间-年
//                    "periodNumber" => date('m'),//会计期间-编码
                            "bookedDate" => $nextDay,//记账日期 充值则是 后推一天
                            "bizDate" => $nextDay,//业务日期
                            "periodYear" => $time[0],//会计期间-年
                            "periodNumber" => $time[1],//会计期间-编码
                            "voucherType" => '收',//凭证字（凭证类型）
                            "attaches" => count($balanceInfo),//附件数量
                            "voucherNumber" => "0044",//凭证号
                            "entrySeq" => count($proof)+count($proof0)+count($proof1)+count($proof2) + 1, //分录行号
                            "voucherAbstract" => $Year[0] . '年' . $Year[1] . "月" . $day[0] . "日" . '收' . $v['merchant']['tenant_name'] . $v['merchant']['positions_code'] . '账户充值' . $brand,
                            "accountNumber" => $accountNumber,
                            "currencyNumber" => "BB01",
                            "entryDC" => $entryDC,
                            "originalAmount" => $originalAmount,
                            "debitAmount" => $debitAmount,
                            "creditAmount" => $creditAmount,
                            "creator" => "桂耀耀",
                            "asstActType1" => "客户",
                            "asstActNumber1" => $asstActNumber1,
                            "asstActName1" => $asstActName1,
                            "asstActType2" => "合同",
                            "asstActNumber2" => "99999999",
                            "asstActName2" => "默认合同",
                            "asstActType3" => $asstActType3,
                            "asstActNumber3" => $asstActNumber3,
                            "asstActName3" => $asstActName3,
                            "asstActType4" => $asstActType4,
                            "asstActNumber4" => $asstActNumber4,
                            "asstActName4" => $asstActName4,
                            "asstActType5" => $asstActType5,
                            "asstActNumber5" => $asstActNumber5,
                            "asstActName5" => $asstActName5,
                            "asstActType6" => $asstActType6,
                            "asstActNumber6" => $asstActNumber6,
                            "asstActName6" => $asstActName6,
                            "asstActType7" => $asstActType7,
                            "asstActNumber7" => $asstActNumber7,
                            "asstActName7" => $asstActName7,
                            "asstActType8" => "",
                            "asstActNumber8" => "",
                            "asstActName8" => ""
                        ];
                        $proof0[] = $arr;
//                        array_unshift($proof,$arr);
                    }
                    //贷方线上线下全部有
                    $accountNumber = '2205.27';
                    $entryDC = 0;
                    $originalAmount = $v['amount'] / 100;
                    $debitAmount = 0;
                    $creditAmount = $v['amount'] / 100;
                    $asstActNumber1 = $v['merchant']['merchant_sn'];
                    $asstActName1 = $v['merchant']['company'];
                    $asstActType3 = '品牌';
                    $asstActNumber3 = '99999999';
                    $asstActName3 = '默认品牌';
                    $asstActType4 = '单元';
                    $asstActNumber4 = '401001A' . $v['merchant']['positions_code'];
                    $asstActName4 = $v['merchant']['positions_name'];
                    $asstActType5 = '业态';
                    $asstActNumber5 = '99999999';
                    $asstActName5 = '默认业态';
                    $asstActType6 = '';
                    $asstActNumber6 = '';
                    $asstActName6 = '';
                    $asstActType7 = '';
                    $asstActNumber7 = '';
                    $asstActName7 = '';
                    $arr = [
                        "companyNumber" => '0109',
//                    "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                    "bizDate" => date('Y-m-01'),//业务日期
//                    "periodYear" => date('Y'),//会计期间-年
//                    "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $nextDay,//记账日期 充值则是 后推一天
                        "bizDate" => $nextDay,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $time[1],//会计期间-编码
                        "voucherType" => '收',//凭证字（凭证类型）
                        "attaches" => count($balanceInfo),//附件数量
                        "voucherNumber" => "0044",//凭证号
                        "entrySeq" => count($proof)+count($proof0)+count($proof1)+count($proof2) + 1, //分录行号
                        "voucherAbstract" => $Year[0] . '年' . $Year[1] . "月" . $day[0] . "日" . '收' . $v['merchant']['tenant_name'] . $v['merchant']['positions_code'] . '账户充值' . $brand,
                        "accountNumber" => $accountNumber,
                        "currencyNumber" => "BB01",
                        "entryDC" => $entryDC,
                        "originalAmount" => $originalAmount,
                        "debitAmount" => $debitAmount,
                        "creditAmount" => $creditAmount,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => $asstActNumber1,
                        "asstActName1" => $asstActName1,
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => $asstActType3,
                        "asstActNumber3" => $asstActNumber3,
                        "asstActName3" => $asstActName3,
                        "asstActType4" => $asstActType4,
                        "asstActNumber4" => $asstActNumber4,
                        "asstActName4" => $asstActName4,
                        "asstActType5" => $asstActType5,
                        "asstActNumber5" => $asstActNumber5,
                        "asstActName5" => $asstActName5,
                        "asstActType6" => $asstActType6,
                        "asstActNumber6" => $asstActNumber6,
                        "asstActName6" => $asstActName6,
                        "asstActType7" => $asstActType7,
                        "asstActNumber7" => $asstActNumber7,
                        "asstActName7" => $asstActName7,
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($onlinefuwu > 0) {
                    $arr = [
                        "companyNumber" => '0109',
                        "bookedDate" => $nextDay,//记账日期 充值则是 后推一天
                        "bizDate" => $nextDay,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $time[1],//会计期间-编码
                        "voucherType" => '收',//凭证字（凭证类型）
                        "attaches" => count($balanceInfo),//附件数量
                        "voucherNumber" => "0044",//凭证号
                        "entrySeq" => count($proof)+count($proof0)+count($proof1)+count($proof2) + 1, //分录行号
//                        "voucherAbstract" =>"结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)).  "水费".'('.$userNumber.')' ,
                        "voucherAbstract" => $Year[0] . '年' . $Year[1] . "月" . $day[0] . "日" . "充值",
                        "accountNumber" => '1122.06.02',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => bcsub($all_amount,$onlinefuwu,2),
                        "debitAmount" => bcsub($all_amount,$onlinefuwu,2),
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认单元",
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof1[] = $arr;
//                    array_splice($proof,0,0,$arr);
//                    array_unshift($proof,$arr);
                }
                if ($onlinefuwu > 0) {
                    $arr = [
                        "companyNumber" => '0109',
                        "bookedDate" => $nextDay,//记账日期 充值则是 后推一天
                        "bizDate" => $nextDay,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $time[1],//会计期间-编码
                        "voucherType" => '收',//凭证字（凭证类型）
                        "attaches" => count($balanceInfo),//附件数量
                        "voucherNumber" => "0044",//凭证号
                        "entrySeq" => count($proof)+count($proof0)+count($proof1)+count($proof2) + 1, //分录行号
//                        "voucherAbstract" =>"结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)).  "水费".'('.$userNumber.')' ,
                        "voucherAbstract" => $Year[0] . '年' . $Year[1] . "月" . $day[0] . "日" . "充值手续费",
                        "accountNumber" => '6603.04',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => $onlinefuwu,
                        "debitAmount" => $onlinefuwu,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "成本中心-部门",
                        "asstActNumber1" => "CW01",
                        "asstActName1" => "财务部",
                        "asstActType2" => "",
                        "asstActNumber2" => "",
                        "asstActName2" => "",
                        "asstActType3" => "",
                        "asstActNumber3" => "",
                        "asstActName3" => "",
                        "asstActType4" => "",
                        "asstActNumber4" => "",
                        "asstActName4" => "",
                        "asstActType5" => "",
                        "asstActNumber5" => "",
                        "asstActName5" => "",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
//                    array_unshift($proof,$arr);
//                    array_splice($proof,0,0,$arr);
                    $proof2[] = $arr;
                }
                $combine = array_merge($proof0,$proof1,$proof2,$proof);
                foreach ($combine as $key=>$val){
                    $combine[$key]['entrySeq'] = $key+1;
                }
                $res = $jin->importVoucher($combine);
                if (isset($res['code']) && $res['code'] == '0000') {
                    $balanceModel->whereBetweenTime('charge_time', strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59'))->where(['is_top_up' => 1, 'status' => 1])->update([
                        'is_push' => 1
                    ]);
                    $balanceModel
                        ->whereBetweenTime('create_time', strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59'))
                        ->whereNull('charge_time')
                        ->where(['is_top_up' => 1, 'status' => 1])->update([
                            'is_push' => 1
                        ]);
                    //线下充值
//                    $balanceModel->where(['merchant_id'=>$data['merchant_id']])->where(['is_top_up'=>1,'status'=>1])->update([
//                        'is_push'=>1
//                    ]);//线下充值
                }
                unset($data['__token__']);
                $data['info'] = $res['message'];
                $data['code'] = $code;
                $data['push'] = '推送' . $data['start_time'] . $type . '凭证';
                $this->model->doAll($data);
            }
            if ($data['type'] == 3) { //水费充值
                $type = '水费';
                $costInfo = (new Cost())->with(['merchant', 'costType'])->whereNotIn('merchant_id',[222,1916,1917,1918])->whereNotNull('pointId')->where('all_amount', '>', 0)->where('settlement_time', $data['start_month'])->where(['is_push' => 0, 'cost_type_id' => 46])->select();
                if ($costInfo->isEmpty()) {
                    return self::ajaxResult([], 0, 0, $data['start_month'] . '没有待推送的水费凭证');
                }
                $code = $costInfo[0]['merchant']['code'];
                $start = $costInfo[0]['settlement_time'];
                $end = $costInfo[count($costInfo) - 1]['settlement_time'];
                $proof = [];
                $BLMoney = 0; //合计不含税水费
                $dianBLMoney = 0; //合计不含税费
                $FWMoney = 0; //合计不含税服务费
                $dianFWMoney = 0; //合计服务费税费
                $userNumber = 0; //水用量
                $allMoney = 0; //累计费用
                foreach ($costInfo as $k => $v) {
                    $brand = '';
                    if (!empty($v['merchant']['name']) && $v['merchant']['name'] != '-') {
                        $brand = '(' . $v['merchant']['name'] . ')';
                    }
                    $userNumber += $v['use_number'];
                    $brand = str_replace('&', '-', $brand);
                    $Year = explode('-', $v['settlement_time']);
                    $voucherType = '转';
                    $voucherAbstract = '水费';
                    $dianfei = TaxMoney($v['all_amount'] / 100, 1, 3);
                    $dianBLMoney += $dianfei['tax_money'];
                    $accountNumber = $v['costType']['cost_sn'];
                    $allMoney += $v['all_amount'] / 100;
                    //是否含有服务费

                    if ($v['unit_price'] != 3.40 && $v['unit_price'] != 9.00) {
                        $serviceMoney = bcmul(3.4, $v['use_number'], 2);
                        $dianfei = TaxMoney($serviceMoney, 1, 3);
                        $BLMoney += $dianfei['money'];
                        $fuwu = bcmul(1.2, $v['use_number'], 2);
                        $FWMoney = TaxMoney($fuwu, 1, 3);
                        $dianFWMoney += $FWMoney['money'];
                    } else {
                        $BLMoney += $dianfei['money'];
                    }
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0100",//凭证号
                        "entrySeq" => $k + 1, //分录行号
                        "voucherAbstract" => "结转" . $Year[0] . '年' . $Year[1] . "月" . $v['merchant']['positions_code'] . $v['merchant']['tenant_name'] . ($v['pointId']) . $voucherAbstract . $brand,
                        "accountNumber" => $accountNumber,
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => $v['all_amount'] / 100,
                        "debitAmount" => $v['all_amount'] / 100,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => $v['merchant']['merchant_sn'],
                        "asstActName1" => $v['merchant']['tenant_name'],
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => '401001A' . $v['merchant']['positions_code'],
                        "asstActName4" => '401001A' . $v['merchant']['positions_name'],
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($BLMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0100",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
//                        "voucherAbstract" =>"结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)).  "水费".'('.$userNumber.')' ,
                        "voucherAbstract" => "结转" . $data['start_month'] . "水费" . '(' . $userNumber . ')',
                        "accountNumber" => '6401.03.01.02',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => '-' . $BLMoney,
                        "debitAmount" => '-' . $BLMoney,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "单元",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认单元",
                        "asstActType4" => "业态",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认业态",
                        "asstActType5" => "成本中心-部门",
                        "asstActNumber5" => "WYCB01",
                        "asstActName5" => "物业成本部",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($dianFWMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0100",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
//                        "voucherAbstract" => "结转" .$Year[0].'年'. $Year[1] . "月服务费税金"  ,
//                        "voucherAbstract" => "结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)).  "水费".'('.$userNumber.')'  ,
                        "voucherAbstract" => "结转" . $data['start_month'] . "水费" . '(' . $userNumber . ')',
                        "accountNumber" => '6401.03.01.03',
                        "currencyNumber" => "BB01",
                        "entryDC" => 1,
                        "originalAmount" => -$dianFWMoney,
                        "debitAmount" => -$dianFWMoney,
                        "creditAmount" => 0,
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认单元",
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "",
                        "asstActNumber6" => "",
                        "asstActName6" => "",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                if ($dianBLMoney > 0) {
                    $arr = [
                        "companyNumber" => '0109',
//                "bookedDate" => date('Y-m-01'),//记账日期 充值则是 后推一天
//                "bizDate" => date('Y-m-01'),//业务日期
//                "periodYear" => date('Y'),//会计期间-年
//                "periodNumber" => date('m'),//会计期间-编码
                        "bookedDate" => $bookedDate,//记账日期 充值则是 后推一天
                        "bizDate" => $bookedDate,//业务日期
                        "periodYear" => $time[0],//会计期间-年
                        "periodNumber" => $month,//会计期间-编码
                        "voucherType" => $voucherType,//凭证字（凭证类型）
                        "attaches" => 1,//附件数量
                        "voucherNumber" => "0100",//凭证号
                        "entrySeq" => count($proof) + 1, //分录行号
//                        "voucherAbstract" => "结转" .$Year[0].'年'. $Year[1] . "月电费税金"  ,
//                        "voucherAbstract" =>"结转" .date('Y年m月',strtotime($start)).'-'.date('Y年m月',strtotime($end)).  "水费".'('.$userNumber.')'  ,
                        "voucherAbstract" => "结转" . $data['start_month'] . "水费" . '(' . $userNumber . ')',
                        "accountNumber" => '2221.01.01',
                        "currencyNumber" => "BB01",
                        "entryDC" => 0,
                        "originalAmount" => bcsub(bcsub($allMoney, $dianFWMoney, 2), $BLMoney, 2),
                        "debitAmount" => 0,
                        "creditAmount" => bcsub(bcsub($allMoney, $dianFWMoney, 2), $BLMoney, 2),
                        "creator" => "桂耀耀",
                        "asstActType1" => "客户",
                        "asstActNumber1" => "99999999",
                        "asstActName1" => "默认客户",
                        "asstActType2" => "合同",
                        "asstActNumber2" => "99999999",
                        "asstActName2" => "默认合同",
                        "asstActType3" => "品牌",
                        "asstActNumber3" => "99999999",
                        "asstActName3" => "默认品牌",
                        "asstActType4" => "单元",
                        "asstActNumber4" => "99999999",
                        "asstActName4" => "默认单元",
                        "asstActType5" => "业态",
                        "asstActNumber5" => "99999999",
                        "asstActName5" => "默认业态",
                        "asstActType6" => "税率",
                        "asstActNumber6" => "3",
                        "asstActName6" => "3%",
                        "asstActType7" => "",
                        "asstActNumber7" => "",
                        "asstActName7" => "",
                        "asstActType8" => "",
                        "asstActNumber8" => "",
                        "asstActName8" => ""
                    ];
                    $proof[] = $arr;
                }
                $res = $jin->importVoucher($proof);
                if (isset($res['code']) && $res['code'] == '0000') {
                    (new Cost())->where('settlement_time', $data['start_month'])->where(['is_push' => 0, 'cost_type_id' => 46])->update([
                        'is_push' => 1
                    ]);
                }
                unset($data['__token__']);
                $data['info'] = $res['message'];
                $data['code'] = $code;
                $data['push'] = '推送' . $data['start_time'] . $type . '凭证';
                $this->model->doAll($data);
            }
            if($data['type']==5){ //月结凭证
                if(empty($data['cost_type_id'])||empty($data['start_time'])){
                    return self::ajaxResult([], 0, 0, '请选择月结凭证费用项/月份');
                }
                $res = $proofService->carryOver($data);
            }
            if($data['type']==6){ //余额缴费
                $data['start_time'] = $data['star_time'];
                if(empty($data['start_time'])||empty($data['end_time'])){
                    return self::ajaxResult([], 0, 0, '请选择开始时间和结束时间');
                }
                $res = $proofService->payBalance($data);
            }
            if($data['type']==7){ //转账缴费
                $data['start_time'] = $data['star_time'];
                if(empty($data['start_time'])||empty($data['end_time'])){
                    return self::ajaxResult([], 0, 0, '请选择开始时间和结束时间');
                }
                $res = $proofService->transferBalance($data);
            }
            if($data['type']==8){ //线上缴费
                $data['start_time'] = $data['star_time'];
                if(empty($data['start_time'])||empty($data['end_time'])){
                    return self::ajaxResult([], 0, 0, '请选择开始时间和结束时间');
                }
                $res = $proofService->onlineBalance($data);
            }
            return self::ajaxResult([], 0, 1, $res['message']);
        }
        //获取商户列表
        $merchant = getMerchant();
        $this->assign("merchant_list", $merchant);
        return $this->fetch();
    }
}